package edu.gyc.test;

import java.util.concurrent.*;

public class HiPool {
    public static void main(String[] args) {
       // ExecutorService pool = Executors.newFixedThreadPool(5);
      //  ExecutorService pool = Executors.newSingleThreadExecutor();
        //makePool();
        System.out.println(Runtime.getRuntime().availableProcessors());
        ExecutorService executorService = new ThreadPoolExecutor(
                2, 5, 1,
                TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(3), Executors.defaultThreadFactory(),
               // new ThreadPoolExecutor.AbortPolicy());
              //  new ThreadPoolExecutor.CallerRunsPolicy());
                new ThreadPoolExecutor.DiscardOldestPolicy());

        try {
            for (int i = 0; i < 10; i++) {
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t办理业务");
                });
               // Thread.sleep(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            executorService.shutdown();
        }
    }

    private static void makePool() {
        ExecutorService pool = Executors.newCachedThreadPool();

        try {
            for (int i = 0; i < 10; i++) {
                pool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t办理业务");
                });
                Thread.sleep(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            pool.shutdown();
        }
    }
}
